//===============================================================================
// Microsoft patterns & practices Enterprise Library Contribution
// Policy Injection Application Block
//===============================================================================

using System.Configuration;
using System.ComponentModel;
using System.Windows.Forms;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using Microsoft.Practices.EnterpriseLibrary.PolicyInjection.Configuration;
using Microsoft.Practices.Unity;
using Microsoft.Practices.Unity.InterceptionExtension;

namespace EntLibContrib.PolicyInjection.CallHandlers.Configuration
{
	/// <summary>
	/// Cursor Call Handler Data
	/// </summary>
	public class CursorCallHandlerData : CallHandlerData
	{
		#region Constants
		private const string CursorPropertyName = "cursor";
		#endregion

		#region Properties
		/// <summary>
		/// The cursor as available on the Cursors class.
		/// </summary>
		/// <value>The "cursor" attribute in configuration</value>
		[ConfigurationProperty(CursorPropertyName, IsRequired = true, DefaultValue = "Default")]
		[TypeConverter(typeof(CursorConverter))]
		public Cursor Cursor
		{
			get { return (Cursor)base[CursorPropertyName]; }
			set { base[CursorPropertyName] = value; }
		}
		#endregion

		#region Construction
		/// <summary>
		/// Construct a new <see cref="CursorCallHandlerData"/>.
		/// </summary>
		public CursorCallHandlerData()
		{
		}

		/// <summary>
		/// Construct a new <see cref="CursorCallHandlerData"/>.
		/// </summary>
		/// <param name="handlerName">Name of the handler.</param>
		public CursorCallHandlerData(string handlerName)
			: base(handlerName, typeof(CursorCallHandler))
		{
		}

		/// <summary>
		/// Construct a new <see cref="CursorCallHandlerData"/>.
		/// </summary>
		/// <param name="handlerName">Name of the handler.</param>
		/// <param name="handlerOrder">The handler order.</param>
		public CursorCallHandlerData(string handlerName, int handlerOrder)
			: base(handlerName, typeof(CursorCallHandler))
		{
			Order = handlerOrder;
		}
		#endregion

		#region Public Methods
		/// <summary>
		/// Adds the call handler represented by this configuration object to <paramref name="policy"/>.
		/// </summary>
		/// <param name="policy">The policy to which the rule must be added.</param>
		/// <param name="configurationSource">The configuration source from which additional information
		/// can be retrieved, if necessary.</param>
		public override void ConfigurePolicy(PolicyDefinition policy, IConfigurationSource configurationSource)
		{
			policy.AddCallHandler<CursorCallHandler>(
					new ContainerControlledLifetimeManager(),
					new InjectionConstructor(
							new InjectionParameter<Cursor>(this.Cursor),
							new InjectionParameter<int>(this.Order)));
		}
		#endregion
	}
}
